package com.dzk.cp.c11x5.index.zx.cs;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.dzk.cp.c11x5.constant.C11x5Attrs;
import com.dzk.cp.c11x5.index.BaseC11x5Index;
import com.dzk.cp.c11x5.pojo.R5;
import com.dzk.cp.c11x5.util.C11x5Utils;
import com.dzk.cp.c11x5.util.SpringBeanUtils;
import com.dzk.cp.util.CpUtils;

public abstract class ZXnCSIndex extends BaseC11x5Index {
	
	public ZXnCSIndex setParams(int code, int num, int unit) {
		super.setParams(code, num, unit);
		return this;
	}
	
	@Override
	public Map<String, Integer> index() {
		Map<String, Integer> resultMap = new LinkedHashMap<String, Integer>();
		List<String> zxcs = getAllZXn();
		List<? extends R5> r5s = SpringBeanUtils.getR5Service(code).queryLatest(getLastestNum());
		for (String zxc : zxcs) {
			resultMap.put(zxc, getCS(r5s, zxc));
		}
		return resultMap;
	}

	public static Integer getCS(List<? extends R5> r5s, String zxc) {
		int sx = 0;
		List<String> zxs = Arrays.asList(zxc.split(" "));
		int n = (zxc.length() + 1 ) / 3;
		for (R5 r5 : r5s) {
			List<String> tmpList = Arrays.asList(new String[] {
					CpUtils.fore2zero(r5.getN1()),
					CpUtils.fore2zero(r5.getN2()),
					CpUtils.fore2zero(r5.getN3()),
					CpUtils.fore2zero(r5.getN4()),
					CpUtils.fore2zero(r5.getN5())}).subList(0, n);
			if (C11x5Utils.contain(tmpList, zxs)) {
				sx++;
			}
		}
		return sx;
	}
	
	protected abstract List<String> getAllZXn();
	
	public static ZXnCSIndex getInstance(int code, int num, int unit, int n) {
		switch (n) {
//		case 1:
//			return new ZHX1CSIndex().setParams(code, num, unit);
		case 2:
			return new ZX2CSIndex().setParams(code, num, unit);
		case 3:
			return new ZX3CSIndex().setParams(code, num, unit);
		default:
			return null;
		}
	}

}

class ZX1CSIndex extends ZXnCSIndex {

	@Override
	protected List<String> getAllZXn() {
		return Arrays.asList(new String[] {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11"});
	}

	@Override
	protected int getZQ() {
		return 11;
	}

}

class ZX2CSIndex extends ZXnCSIndex {

	@Override
	protected List<String> getAllZXn() {
		return C11x5Attrs.getAllZX2();
	}

	@Override
	protected int getZQ() {
		return 110 / 2;
	}

}

class ZX3CSIndex extends ZXnCSIndex {

	@Override
	protected List<String> getAllZXn() {
		return C11x5Attrs.getAllZX3();
	}

	@Override
	protected int getZQ() {
		return 990 / 6;
	}

}
